package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.Comments;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.CommentsMapper;
import com.lzx.article.service.CommentsService;
import com.lzx.article.vo.NicknameVO;
import com.lzx.article.vo.OssVO;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-01-20
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private UserClient userClient;

    //用户发表评论
    @Transactional
    @Override
    public R publishComments(Comments comments) {
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);

        //文章评论数+1
        Articles articles = articlesMapper.selectById(comments.getArticleId());
        articles.setCommentCount(articles.getCommentCount() + 1);
        articlesMapper.updateById(articles);

        //发送通知
        Inform inform = new Inform();
        inform.setUserId(articles.getUserId());
        inform.setSender(Math.toIntExact(comments.getUserId()));
        inform.setNickname(comments.getNickname());
        inform.setAvatar(comments.getAvatar());
        inform.setType(InformType.COMMENTARTICLE.getType());
        inform.setMessage(articles.getTitle());
        userClient.saveInform(inform);

        return R.ok().message("评论成功！");
    }

    //修改用户头像(文章一级评论)
    @Transactional
    @Override
    public String updateAvatarToComments(OssVO ossVO) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ossVO.getId());
        List<Comments> commentsList = commentsMapper.selectList(wrapper);
        for (Comments comments : commentsList) {
            comments.setAvatar(ossVO.getAvatar());
            commentsMapper.updateById(comments);
        }
        return "修改成功";
    }

    //修改用户昵称(文章一级评论)
    @Transactional
    @Override
    public String updateNicknameToComments(NicknameVO nicknameVO) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", nicknameVO.getId());
        List<Comments> commentsList = commentsMapper.selectList(wrapper);
        for (Comments comments : commentsList) {
            comments.setNickname(nicknameVO.getNickname());
            commentsMapper.updateById(comments);
        }
        return "修改成功";
    }
}
